﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using Dcms.Common;
using Dcms.BLL;
using Dcms.Model;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_UserManage_EditPermission : System.Web.UI.Page
{
    Dcms_PermissionBLL pBll = new Dcms_PermissionBLL();
    public string Language = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        CompetenceLib CL = new CompetenceLib();
        CL.IsLogin();
        if (Session["DCMSLANGUAGE"] != null)
        {
            Language = Session["DCMSLANGUAGE"].ToString();
        }
        int ID = DcmsRequest.GetQueryInt("ID", 0);
        if (!IsPostBack)
        {
            if (ID == 0)
            {
                this.ddl_PermissionParentID.DataSource = pBll.DDLPermissionData();
                this.ddl_PermissionParentID.DataTextField = "Permission_Name";
                this.ddl_PermissionParentID.DataValueField = "Permission_Id";
                this.ddl_PermissionParentID.DataBind();
                this.ddl_PermissionParentID.Items.Add(new ListItem("作为一级分类", "0"));
                this.ddl_PermissionParentID.SelectedValue = "0";
            }
            else
            {
                Dcms_PermissionModel pModel = new Dcms_PermissionModel();
                pModel = pBll.GetModelByID(ID);
                this.ddl_PermissionParentID.DataSource = pBll.DDLPermissionData();
                this.ddl_PermissionParentID.DataTextField = "Permission_Name";
                this.ddl_PermissionParentID.DataValueField = "Permission_Id";
                this.ddl_PermissionParentID.DataBind();
                this.ddl_PermissionParentID.Items.Add(new ListItem("作为一级分类", "0"));
                this.ddl_PermissionParentID.SelectedValue = pModel.Permission_ParentID.ToString();
                this.txt_PermissionUrl.Text = pModel.Permission_Url;
                this.txt_PermissionOrder.Text = pModel.Permission_Order.ToString();
                this.txt_PermissionName.Text = pModel.Permission_Name;
                this.txt_PermissionFlag.Text = pModel.Permission_Flag;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Dcms_PermissionModel pModel = new Dcms_PermissionModel();
        int ID = DcmsRequest.GetQueryInt("ID", 0);
        if (DcmsRequest.GetFormInt("ddl_PermissionParentID", 0) > 0)
        {
            pModel.Permission_Flag = this.txt_PermissionFlag.Text.ToUpper();
            pModel.Permission_Layer = 1;
        }
        else
        {
            pModel.Permission_Flag = this.txt_PermissionFlag.Text;
            pModel.Permission_Layer = 0;
        }
        pModel.Permission_AllAction = "Read|Add|Edit|Delete";
        pModel.Permission_Name = this.txt_PermissionName.Text;
        pModel.Permission_Order = Convert.ToInt32(this.txt_PermissionOrder.Text);
        pModel.Permission_ParentID = DcmsRequest.GetFormInt("ddl_PermissionParentID",0);
        pModel.Permission_Url = this.txt_PermissionUrl.Text;

        Dcms_CateBLL cBll = new Dcms_CateBLL();
        
        if (ID == 0)
        {
            //取得同个模块最大的ModelKeyID
            Dcms_PermissionBLL bllPermission = new Dcms_PermissionBLL();

            int intModelKeyId = bllPermission.GetMaxModelKeyID(pModel.Permission_Flag) + 1;
            pModel.Permission_ModelKeyId = intModelKeyId;

            pBll.AddModel(pModel);

            //将对应模块默认的内容存入到ModelField表中
            Dcms_ModelFieldBLL mBll = new Dcms_ModelFieldBLL();
            mBll.CopyDefaultModelByModel(pModel.Permission_Flag, intModelKeyId);

            //同步栏目
            if (Convert.ToInt32(rad_IsUpDateCate.SelectedValue.ToString()) == 1)
            {
                Dcms_CateModel cModel = new Dcms_CateModel();
                cModel.Cate_AddTime = DateTime.Now;
                cModel.Cate_Flag = this.txt_PermissionFlag.Text.ToString().ToUpper();
                cModel.Cate_ID = 0;
                cModel.Cate_Image = "";
                cModel.Cate_Intro = this.txt_PermissionName.Text.ToString();
                cModel.Cate_Lang = Language;
                cModel.Cate_Name = this.txt_PermissionName.Text.ToString();
                cModel.Cate_OpType = "ADDONLY";
                cModel.Cate_Order = 3;
                cModel.Cate_ParentID = 0;
                cModel.Cate_Key = "";
                cModel.Cate_ModelKeyId = pModel.Permission_ModelKeyId;
                cBll.AddModel(cModel);
            }
            Session.Add("Message", "新功能模块已添加，请刷新查看");
        }
        else
        {
            pModel.Permission_Id = ID;
            pBll.Update(pModel);
            //同步栏目
            if (Convert.ToInt32(rad_IsUpDateCate.SelectedValue.ToString()) == 1)
            {
                Dcms_CateModel cModel = new Dcms_CateModel();
                cModel = cBll.GetModelByFlag(this.txt_PermissionFlag.Text.ToUpper());
                if (cModel != null)
                {
                    cModel.Cate_Flag = this.txt_PermissionFlag.Text.ToUpper();
                    cModel.Cate_Name = this.txt_PermissionName.Text.ToString();
                    cBll.Update(cModel);
                }
            }
            Session.Add("Message", "编辑功能模块已添加，请刷新查看");
        }

        Session.Add("Url", "Modle/ModelField/Permission/PermissionList.aspx?id=" + strPermissionID);
        Response.Redirect("../../../Message.aspx",false);
    }

    protected string strPermissionID
    {
        get
        {
            return Request.QueryString["PermissionID"] + "";
        }
    }
}